using System;
using Lucene.Linq.Mapping;
using Lucene.Linq.Storage;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;

namespace Lucene.Linq.Demo3_Azure
{
    [Serializable, Document(DefaultAnalyzer = typeof(StandardAnalyzer))]
    internal class Person : IEntity
    {
        [Field(FieldIndex.UnTokenized, FieldStore.Yes, Analyzer = typeof(SimpleAnalyzer), IsDefault = false)]
        public string Id { get; set; }

        [Field(FieldIndex.Tokenized, FieldStore.Yes, Analyzer = typeof(SimpleAnalyzer), IsDefault = false)]
        public string Name { get; set; }

        [Field(FieldIndex.No, FieldStore.Yes, Analyzer = typeof(SimpleAnalyzer), IsDefault = false)]
        public string SomeFieldThatIsNotStored { get; set; }

        /// <summary>
        /// Default constructor, we need this when we serialize and especially deserialize objects
        /// </summary>
        public Person()
        { }

        /// <summary>
        /// Specialized constructor, so I can show the example with very few lines of code
        /// </summary>
        /// <param name="id">id of person</param>
        /// <param name="name">name of person</param>
        public Person(string id, string name)
        {
            Id = id;
            Name = name;
            SomeFieldThatIsNotStored = string.Format("{0} ! {1}", id, name);
        }

        /// <summary>
        /// Content type for entity
        /// </summary>
        public string ContentType
        {
            get { return Storage.ContentType.Binary; }
            set
            {
                // nothing to do
            }
        }

        /// <summary>
        /// Key of entity
        /// </summary>
        [Field(FieldIndex.UnTokenized, FieldStore.Yes, IsKey = true, IsDefault = true, Analyzer = typeof(SimpleAnalyzer))]
        public string Key
        {
            get { return MakeKey(Id); }
            set
            {
                // do nothing... But this is required because of deserialization
            }
        }

        /// <summary>
        /// Just make some string that we can use as a key
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static string MakeKey(string Id)
        {
            return string.Format("person{0}", Id);
        }
    }
}